<?php
namespace Tongcheng\Controller;
use Think\Controller;

/**
 * Class ShopOrderBackController
 * @package Tongcheng\Controller
 *
 * 订单管理
 */
class ShopOrderBackController extends CommonController {
    /**
     * 获取订单列表数据
     */
    public function index(){
        $model = M("ShopOrder");
        $map = array();
//        $map['wid'] = session('wid');
//        $map['status'] = 1;
        // $map['is_pay'] = 1;

        $res['count'] = $model->where($map)->count();
        $page = $_GET['page']?$_GET['page']:1;
        $limit = $_GET['limit']?$_GET['limit']:10;
        $list = $model->field("tp_shop_order.*,s.nickname,FROM_UNIXTIME(tp_shop_order.create_time,\"%m-%d %H:%i\") time")->join("tp_member s on s.uid = tp_shop_order.uid",'left')->where()->page("{$page},{$limit}")->order('id desc')->select();
        //处理数据获取订单商品详情
        if(!empty($list)){
            foreach ($list as &$item){
                if(!empty($item)){
                    //购买图书
                    if(1 == $item['type']){
                        $goodinfo = $model->query('select * from tp_books where id = '.$item['otherid']);
                        if(!empty($goodinfo[0])){
                            $item['goodinfo'] = $goodinfo[0];
                        }else{
                            $item['goodinfo'] = array('book_name'=>'');
                        }
                    }else if(2 == $item['type']){//购买闲置
                        $goodinfo = $model->query('select * from tp_unuse tu where tu.id = '.$item['otherid']);
                        if(!empty($goodinfo[0])){
                            $item['goodinfo'] = $goodinfo[0];
                        }else{
                            $item['goodinfo'] = array('unuse_name'=>'');
                        }
                    }

                }
            }
        }
        unset($item);
        $res['list']= $list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('res', $res);
            $this->display();
        }
    }

    /**
     * 查看订单信息
     */
    public function edit(){
        $model = M("ShopOrder");
        $id = $_GET['id'];
        $list = $model->field('tp_shop_order.*,tp_shop_order.address oaddress,tp_shop_order.create_time screate_time, tp_member.*,FROM_UNIXTIME(tp_shop_order.create_time,"%m-%d %H:%i") time')->join('tp_member on tp_shop_order.uid = tp_member.uid','left')->where(array('tp_shop_order.id' => $id))->find();
        //处理数据获取订单商品详情
        if(!empty($list)){
            //购买图书
            if(1 == $list['type']){
                $goodinfo = M('')->query('select * from tp_books where id = '.$list['otherid']);
                if(!empty($goodinfo[0])){
                    $list['goodinfo'] = $goodinfo[0];
                }
            }else if(2 == $list['type']){//购买闲置
                $goodinfo = M('')->query('select * from tp_unuse tu where tu.id = '.$list['otherid']);
                if(!empty($goodinfo[0])){
                    $list['goodinfo'] = $goodinfo[0];
                }
            }
        }
        $this->assign('list', $list);
        $this->display();
    }

    //发货页面
    public function fahuo(){
        $id = $_GET['id'];
        $kuaidi_sn = $_GET['kuaidi_sn'];
        $kuaidi_com = $_GET['kuaidi_com'];
        $res['id']= $id;
        $this->assign('res', $res);
        $this->display();
    }

    //确认发货
    public function querenfahuo(){
        $id = $_POST['id'];
        $_POST['status'] = 3;
        $result = $model = M('ShopOrder')->where('id = '.$id)->save($_POST);
        if($result){//发货成功
            $this->ajaxReturn(1);
        }else{//发货失败
            $this->ajaxReturn(0);
        }

    }

    /**
     * 删除订单
     */
    public function del(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(0);
        }
        $result = M('ShopOrder')->where('id = '.$id)->delete();
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
}